// pages/client/client-contract/client-contract.js
import Dialog from '../../../miniprogram_npm/@vant/weapp/dialog/dialog';
import { getContractInfo,sendAddContract } from '../../../network/client';
import { toast,format } from '../../../utils/Tools';
Page({
  data: {
    customer_id:'',
    agreement_id:'',
    customer_name:'',
    supplyer_company:'',
    get_company:'',
    agreement_code:null,
    isSeal: false,
    isPopup: false,
    minDate:new Date().getTime(),
    actions: [
      { name: '企业公章'},
      { name: '电子印章'}
    ],
    tableList:[{standards:'',product_address:'',total_sell:'',product_num:'',mark:''}],
    formInfo:null,
    date:null,
    dateVal:'暂无日期'
  },
  onLoad: function (options) {
    this.setData({
      agreement_id:options.agreement_id||null,
      agreement_code: options.agreement_code||null,
      customer_id:options.customer_id||null,
      customer_name: options.customer_name||null
    })
    //     调用网络请求
    if(options.agreement_id) this._getContractInfo(options.agreement_id)
  },
  //       函数事件      //
  forbidScroll(e) {
    return;
  },
  handleNameInput(e){
    const {type} = e.currentTarget.dataset
    const value = `${type}_company`
    switch(type){
      case 'supplyer':
        this.setData({[value]:e.detail.value})
        break;
      case 'get':
        this.setData({[value]:e.detail.value})
        break;
    }
  },
  handleTableListInput(e){ //  处理表单列表输入框输入
    const {index,box} = e.currentTarget.dataset
    const value = `tableList[${index}].${box}`
    this.setData({
      [value]: e.detail.value
    })
  },
  handleTableDele(e){ //  点击处理添加删除表单行
    const {type} = e.currentTarget.dataset
    switch(type){
      case 'add':
        this.setData({
          tableList:this.data.tableList.concat({standards:'',product_address:'',total_sell:'',product_num:'',mark:''})
        })
        break;
      case 'cut':
        if(this.data.tableList.length<=1){
          wx.showToast({
            title: '最少输入一行内容',
            icon: 'none'
          });
          return;
        };
        this.data.tableList.pop();
        this.setData({tableList:this.data.tableList})
        break;
    }
  },
  bindDateChange(e){  //  处理选择日期
    this.setData({date: e.detail,dateVal:format(e.detail),isPopup: false})
  },
  handleSeal(){ //处理选择公章
    this.setData({
      isSeal: true
    })
  },
  handlePopup(){  //  点击了时间选择
    this.setData({
      isPopup: true
    })
  },
  onClose() {  //   处理关闭选择盖章
    this.setData({ isSeal: false,isPopup: false });
  },
  formSubmit(e){  //   提交表单数据
    this.setData({
      formInfo: e.detail.value
    })
  },
  onSelect(e){  //处理选择盖章
    const {name} = e.detail
    //  数据的处理
    const handleDate = {
      agreement_method:name,
      customer_name:this.data.customer_name,
      customer_id:this.data.customer_id,
      sign_date:this.data.dateVal,
      product: JSON.stringify(this.data.tableList),
      date_day: this.data.formInfo.date_day || '5',
      this_month: this.data.formInfo.this_month || '1',
      next_month: this.data.formInfo.next_month || '31',
      settlement_month: this.data.formInfo.settlement_month || '上月',
      payment: this.data.formInfo.payment || '100%',
      settlement_nextMonth: this.data.formInfo.settlement_nextMonth || '次月',
      tardy: this.data.formInfo.tardy || '1.8%',
      contractInfo_one: this.data.formInfo.contractInfo_one || '贰',
      contractInfo_two: this.data.formInfo.payment || '壹',
      contractInfo_three: this.data.formInfo.payment || '壹'
    }
    let data;
    if(this.data.agreement_id){
      data = Object.assign(this.data.formInfo,handleDate,{agreement_id:this.data.agreement_id})
    }else{
      data = Object.assign(this.data.formInfo,handleDate)            
    }
    Dialog.confirm({
        title: '您已签约成功',
        message: '是否现在提交合同'
      }).then(() => {
        if(data.receiver_phone&&data.person_phone&&data.receiver){
          sendAddContract(data).then(res=>{
            if(res.code === 200){
              toast('提交合同成功','success')
              setTimeout(()=>{
                wx.navigateBack({
                  delta: 1
                });
              },1500)
            }else{
              toast('提交合同失败')
            }
          }).catch(err=>{
            console.warn(err)
          })
        }else{
          toast('你输入有空，请重新填写')
        }
      }).catch(() => {
        toast('取消操作')
      });
  },
  //      网络请求      //
  _getContractInfo(data){
    getContractInfo(data).then(res=>{
      if(res.code === 200){
        this.setData({
          tableList:res.data.product,
          agreement_id:res.data.agreement_id
        })
      }
    }).catch(err=>{
      console.warn(err)
    })
  }
})